// eslint-disable-next-line
import { dictionaryByType, gpuOfStationInfo, stationAll, externalType, monthlyFee, hourFee, mirroringAll, cloudServerAdd, orderInfo } from '@/api/Product/cloudServer'

const cloudServer = {
  state: {
    // 字典表集合
    dictionary: {
      GPU_TYPE: [],
      CPU_TYPE: [],
      NETWORK_TYPE: [],
      STORAGE_TYPE: [],
      MEMORY_TYPE: [],
      OS_VERSION: [],
      OS: [],
      AI_VERSION: [],
      AI: []
    },
    configureInfo: {
      stationTab: null, // stationList里面的item，站点名称为item.name
      configureBase: null, // 规格配置，是选中的item对象，包含名称、GPU等
      aiImageType: null, // AI框架
      aiImageVersion: null, // AI版本信息
      sysImageType: null, // 系统
      sysImageVersion: null, // 系统版本信息
      storageType: null, // 存储
      storagSize: null, // 存储大小
      flexibleIp: null, // 弹性IP信息的M数，没选则为null
      serverName: '' // 服务器名称
    }, // 基础配置信息
    loadingGpu: false, // gpu列表查询的转圈
    // 所有站点
    stationAll: [],
    gpuTabList: [], //  一个站点下gpu的tab标签集合
    gpuList: [], // gpu列表
    monthlyFee: null, // 按月的费用
    hourFee: null, // 按量的费用
    mirroringAll: null,
    orderAdd: null, // 新增订单
    orderInfo: null, // 订单信息查询
    productId: null, // 产品id
    productName: null // 产品名称
  },

  mutations: {
    SET_dictionaryByType: (state, data) => {
      state.dictionary[data.type] = data.data
    },
    SET_ConfigureInfo: (state, data) => {
      state.configureInfo = data
    },
    SET_OrderAdd: (state, data) => {
      state.orderAdd = data
    },
    SET_LoadingGpu: (state, data) => {
      state.loadingGpu = data
    },
    SET_stationAll: (state, data) => {
      state.stationAll = data
    },
    SET_gpuTabList: (state, data) => {
      state.gpuTabList = data
    },
    SET_GpuList: (state, data) => {
      state.gpuList = data
    },
    SET_monthlyFee: (state, data) => {
      state.monthlyFee = data
    },
    SET_hourFee: (state, data) => {
      state.hourFee = data
    },
    SET_mirroringAll: (state, data) => {
      state.mirroringAll = data
    },
    SET_OrderInfo: (state, data) => {
      state.orderInfo = data
    },
    SET_ProductInfo: (state, data) => {
      state.productId = data.productId !== undefined && data.productId !== null ? Number(data.productId) : null
      state.productName = data.productName
    }
  },

  actions: {
    // 查询字典表获取资源类型
    dictionaryByType({ commit }) {
      const type = [
        'GPU_TYPE',
        'CPU_TYPE',
        'NETWORK_TYPE',
        'MEMORY_TYPE',
        'STORAGE_TYPE',
        'OS_VERSION',
        'OS',
        'AI_VERSION',
        'AI'
      ]
      type.map(item => {
        return new Promise((resolve, reject) => {
          dictionaryByType({ typeCode: item })
            .then(response => {
              if (response && response.data) {
                commit('SET_dictionaryByType', { type: item, data: response.data })
              }
              resolve()
            })
            .catch(error => {
              reject(error)
            })
        })
      })
    },
    /**
     * @param parameter
     * @returns {*}
     */
    // 更新配置详情
    updateConfigureInfo({ commit }, obj) {
      commit('SET_ConfigureInfo', obj)
    },
    // 更新订单信息
    updateOrderAdd({ commit }, obj) {
      commit('SET_OrderAdd', obj)
    },
    // 更新产品信息
    updateProductInfo({ commit }, obj) {
      commit('SET_ProductInfo', obj)
    },
    // 更新产品信息
    updateGpuTabList({ commit }, obj) {
      commit('SET_gpuTabList', obj)
    },
    // 查询所有站点数据
    getSationList({ commit }, obj) {
      return new Promise((resolve, reject) => {
        stationAll(obj)
          .then(response => {
            if (response && response.data) {
              commit('SET_stationAll', response.data)
            }
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 查询产品站点对应的去重的硬件类型
    externalType({ commit }, obj) {
      return new Promise((resolve, reject) => {
        externalType(obj)
          .then(response => {
            if (response && response.data) {
              commit('SET_gpuTabList', response.data)
            }
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 查询出站点下有关型号的所有GPU数据
    gpuOfStationInfo({ commit }, obj) {
      commit('SET_LoadingGpu', true)
      return new Promise((resolve, reject) => {
        gpuOfStationInfo(obj)
          .then(response => {
            const data = response.data && response.data.rows ? response.data.rows : []
            if (data) {
              commit('SET_GpuList', data)
            }
            resolve(response)
            commit('SET_LoadingGpu', false)
          })
          .catch(error => {
            reject(error)
            commit('SET_LoadingGpu', false)
          })
          .finally(() => {
            commit('SET_LoadingGpu', false)
          })
      })
    },
    // GPU云服务器-包年包月计费
    monthlyFee({ commit }, obj) {
      return new Promise((resolve, reject) => {
        monthlyFee(obj)
          .then(response => {
            if (response && response.data) {
              commit('SET_monthlyFee', response.data)
            }
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // GPU服务器购买-按量计费
    hourFee({ commit }, obj) {
      return new Promise((resolve, reject) => {
        hourFee(obj)
          .then(response => {
            if (response && response.data) {
              commit('SET_hourFee', response.data)
            }
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    mirroringAll({ commit }, obj) {
      return new Promise((resolve, reject) => {
        mirroringAll(obj)
          .then(response => {
            if (response && response.data) {
              commit('SET_mirroringAll', response.data)
            } else {
              commit('SET_mirroringAll', null)
            }
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    cloudServerAdd({ commit }, obj) {
      return new Promise((resolve, reject) => {
        cloudServerAdd(obj)
          .then(response => {
            if (response && response.data) {
              commit('SET_OrderAdd', response.data)
            } else {
              commit('SET_OrderAdd', null)
            }
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 查询订单信息
    orderInfo({ commit }, obj) {
      return new Promise((resolve, reject) => {
        orderInfo(obj)
          .then(response => {
            if (response && response.data) {
              commit('SET_OrderAdd', response.data)
            } else {
              commit('SET_OrderAdd', null)
            }
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default cloudServer
